package com.bestvike.stone.spring.trace.rabbit;

import com.bestvike.stone.core.lang.StringUtils;
import com.bestvike.stone.core.lang.ThrowableUtils;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.amqp.core.Message;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by 许崇雷 on 2017-10-14.
 */
@CommonsLog
public final class OutgoingLog {
    public static Map<String, Object> writeBeginLog(Message message, String exchange, String routingKey) {
        String msgId = RabbitLogUtils.getMsgId(message);
        Map<String, Object> map = new LinkedHashMap<>();
        if (StringUtils.isNotEmpty(msgId))
            map.put("msgId", msgId);
        map.put("exchange", exchange);
        map.put("routingKey", routingKey);
        map.put("messageHeaders", RabbitLogUtils.getHeaders(message));
        map.put("messageBody", RabbitLogUtils.getBody(message));
        return map;
    }

    public static void writeEndLog(Map<String, Object> map, long tookMs) {
        map.put("result", "成功");
        map.put("took", tookMs);
        log.info("==>Rabbit Produce: " + map);
    }

    public static void writeErrorLog(Map<String, Object> map, Exception e, long tookMs) {
        map.put("error", ThrowableUtils.getMessage(e));
        map.put("took", tookMs);
        log.error("==>Rabbit Produce Error: " + map);
    }
}
